package com.glory.hikvisionmobile.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.glory.hikvisionmobile.R;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/1/8 13:51
 * @DESC *****************************
 */
public class HikLabelTextView extends LinearLayout {

    private String labelStr;
    private float labelSize;
    private int labelColor;
    private float labelContentSize;
    private int labelContentColor;
    private int labelGravity;
    private int labelWeight;
    private int labelContentWeight;
    private String labelContentHint;
    private int labelContentHintColor;
    private int labelContentType;
    private int rightIcon;

    private TextView label;
    private TextView labelContext;

    public HikLabelTextView(Context context) {
        super(context);
        init(null, 0);
    }

    public HikLabelTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HikLabelTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            throw new IllegalArgumentException("must be init by xml");
        }
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.HikLabelTextView, defStyle, 0);
        labelStr = array.getString(R.styleable.HikLabelTextView_label);
        if (labelStr == null) {
            labelStr = "Label";
        }
        labelSize = array.getDimension(R.styleable.HikLabelTextView_labelSize, 0);
        labelColor = array.getColor(R.styleable.HikLabelTextView_labelColor, Color.parseColor("#000000"));
        labelContentSize = array.getDimension(R.styleable.HikLabelTextView_labelContentSize, 0);
        labelContentColor = array.getColor(R.styleable.HikLabelTextView_labelContentColor, Color.parseColor("#000000"));
        labelGravity = array.getInt(R.styleable.HikLabelTextView_labelGravity, 0);
        labelWeight = array.getInt(R.styleable.HikLabelTextView_labelWeight, 0);
        labelContentWeight = array.getInt(R.styleable.HikLabelTextView_labelContentWeight, 0);
        labelContentHint = array.getString(R.styleable.HikLabelTextView_labelContentHint);
        labelContentHintColor = array.getColor(R.styleable.HikLabelTextView_labelContentHintColor, Color.parseColor("#999999"));
        labelContentType = array.getInt(R.styleable.HikLabelTextView_labelContentType,0);
        rightIcon = array.getResourceId(R.styleable.HikLabelTextView_rightIcon, 0);
        array.recycle();

        inflate();
    }

    private void inflate() {
        inflate(getContext(), R.layout.hik_view_label_textview, this);
        label = findViewById(R.id.tv_label);
        labelContext = findViewById(R.id.tv_text);

        label.setText(labelStr);
        if (labelSize > 0) {
            label.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        }
        label.setTextColor(labelColor);
        LayoutParams params1 = (LayoutParams) label.getLayoutParams();
        if (labelGravity == 0) {
            params1.gravity = Gravity.CENTER_VERTICAL;
        }
        if (labelWeight > 0) {
            params1.weight = labelWeight;
        }
        label.setLayoutParams(params1);

        labelContext.setHintTextColor(labelContentHintColor);
        labelContext.setTextColor(labelContentColor);
        labelContext.setHint(labelContentHint);
        if (labelContentSize > 0) {
            labelContext.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelContentSize);
        }
        LayoutParams params2 = (LayoutParams) labelContext.getLayoutParams();
        if (labelContentWeight > 0) {
            params2.weight = labelContentWeight;
        }
        labelContext.setLayoutParams(params2);
        if (labelContentType == 1) {
            labelContext.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.hik_ic_expand_go, 0);
        }
        if (rightIcon != 0) {
            labelContext.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightIcon, 0);
        }
    }

    public TextView getLabelContext() {
        return labelContext;
    }
}
